using System;
using System.Collections;
using System.Linq;
using System.Web.Mvc;
using Ornament.MemberShip;
using Ornament.MVCWebFrame.Models;
using Ornament.Web.Mvc.JQuery.JQGrid;
using Ornament.Web;

namespace Ornament.MVCWebFrame.Areas.MemberShips.Controllers
{
    [Authorize(Roles = "admin")]
    public class UsergroupsController : Controller
    {
        //
        // GET: /Usergroups/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List([ModelBinder(typeof(JQGridSubmitDataBinder))] JQGridSubmitData submitData)
        {
            JQGridResult result = submitData.Create();
            result.records = (from usergroup in OrnamentContext.Current.MemberShipFactory.UserGroups
                              select usergroup).Count();

            IQueryable<UserGroup> userGroups = from usergroup in OrnamentContext.Current.MemberShipFactory.UserGroups
                                               select usergroup;
            foreach (UserGroup ug in userGroups)
            {
                result.rows.Add(new JQItem
                                    {
                                        id = ug.Id.ToString(),
                                        cell = new ArrayList
                                                   {
                                                       ug.Name,
                                                       ug.Comment
                                                   }
                                    });
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Usergroups/Details/5
        public ActionResult Details(Guid? id)
        {
            UserGroup ug = OrnamentContext.Current.MemberShipFactory.CreateUserGroupDao().Get(id.Value);
            return Json(ug);
        }

        //
        // GET: /Usergroups/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Usergroups/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            var ug = new UserGroup(collection["Name"]) {Comment = collection["Comment"] };
            OrnamentContext.Current.MemberShipFactory.CreateUserGroupDao().SaveOrUpdate(ug);
            return RedirectToAction("Index");
        }

        //
        // GET: /Usergroups/Edit/5

        public ActionResult Edit(Guid? id)
        {
            UserGroup ug = OrnamentContext.Current.MemberShipFactory.CreateUserGroupDao().Get(id.Value);
            return View(ug);
        }

        //
        // POST: /Usergroups/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid? id, FormCollection collection)
        {
            try
            {
                UserGroup ug = OrnamentContext.Current.MemberShipFactory.CreateUserGroupDao().Get(id.Value);
                ug.Name = collection["Name"];
                ug.Comment = collection["Comment"];
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}